package com.itheima.health.util;

import lombok.extern.slf4j.Slf4j;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

@Component
@Slf4j
public class PasswordUtil {

    /**
     * MD5加密
     * Decryption
     *
     * @param password
     * @return
     */
    public String md5Password(String password) {
        String passwordMd5 = "{md5}" + DigestUtils.md5DigestAsHex(password.getBytes());
        return passwordMd5;
    }

    /**
     * MD5判断
     *
     * @param
     * @return
     */
    public boolean md5PasswordDecryption(String password, String passwordDb) {
        String passwordMd5 = "{md5}" + DigestUtils.md5DigestAsHex(password.getBytes());
        return passwordMd5.equals(passwordDb);
    }

    /**
     * bcrypt加密
     *
     * @param password
     * @return
     */
    public String bcryptPassword(String password) {
        String pwd = "{bcrypt}" + BCrypt.hashpw(password, BCrypt.gensalt());
        return pwd;
    }

    /**
     * bcrypt判断
     *
     * @param password
     * @return
     */
    public boolean bcryptPasswordDecryption(String password, String passwordDb) {
        String newPwd = passwordDb.substring(8);
        return BCrypt.checkpw(password, newPwd);
    }
}
